//package com.welike.shibo.controller.admin;
//
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.welike.shibo.controller.BaseController;
//import com.welike.shibo.eneity.Admin;
//import com.welike.shibo.eneity.JsonResult;
//import com.welike.shibo.eneity.Log;
//import com.welike.shibo.enums.ResultEnum;
//import com.welike.shibo.service.AdminService;
//import com.welike.shibo.service.AudioService;
//import com.welike.shibo.service.LogService;
//import com.welike.shibo.utils.PasswordEncrypt;
//import com.welike.shibo.utils.ResultUtils;
//import com.welike.shibo.vo.IndexDataVO;
//import io.swagger.annotations.ApiParam;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
//import org.springframework.web.bind.annotation.*;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.Date;
//
///**
// * @author fangyu
// * @version v1.0.0
// * @since 2019-08-24 00:29
// */
//
//@Slf4j
//@Controller
//@RequestMapping(value = "/admin")
//public class AdminIndexController extends BaseController {
//
//    @Resource
//    private AdminService adminService;
//
//    @Resource
//    private AudioService audioService;
//
//    @Autowired
//    private LogService logService;
//
//    @GetMapping(value = "/login")
//    public String login() {
//        return "admin/login";
//    }
//
//    @GetMapping(value = "/logout")
//    public String logout() {
//        emptySession(null);
//        return "admin/login";
//    }
//
//    /**
//     * 个人设置页面
//     */
//    @GetMapping(value = "/profile")
//    public String profile() {
//        Admin admin = (Admin) getUser4Session();
//        if (admin == null){
//            return "admin/login";
//        }
//        return "admin/profile";
//    }
//
//
//    @PostMapping(value = "/password")
//    @ResponseBody
//    public JsonResult updatePwd(
//            @RequestParam String oldPassword,
//            @RequestParam String newPassword
//    ){
//        map.clear();
//        try {
//            Admin admin = (Admin) getUser4Session();
//            if (admin == null){
//                return ResultUtils.error(ResultEnum.NOT_LOGIN.getCode(), ResultEnum.NOT_LOGIN.getMsg());
//            }
//            if (StringUtils.isBlank(oldPassword) || StringUtils.isBlank(newPassword)) {
//                return ResultUtils.error("请确认信息输入完整");
//            }
//            log.info("admin ==> {}", admin);
//            if (!admin.getPassword().equals(PasswordEncrypt.encodeByMd5(oldPassword))) {
//                return ResultUtils.error(ResultEnum.PASSWORD_ERROR.getCode(), "密码不正确");
//            }
//            if (newPassword.length() < 6 || newPassword.length() > 14) {
//                return ResultUtils.error("请输入6-14位密码");
//            }
//            admin.setPassword(PasswordEncrypt.encodeByMd5(newPassword));
//            adminService.updateById(admin);
//        } catch (Exception e){
//            log.info("Error ==> {}", e.getMessage());
//            return ResultUtils.error("密码修改失败");
//        }
//        map.put("path", "/api/user/modify_pwd");
//        return ResultUtils.success("密码修改成功!", map);
//    }
//
//    @PostMapping(value = "/login")
//    @ResponseBody
//    public JsonResult toLogin(
//            HttpServletRequest request,
//            HttpServletResponse response,
//            @ApiParam(name = "username", value = "用户名", required = true)
//            @RequestParam(name = "username", required = true)
//                    String username,
//            @ApiParam(name = "password", value = "密码", required = true)
//            @RequestParam(name = "password", required = true)
//                    String password
//    ) {
//        try {
//            Admin admin = adminService.login(username, password);
//            log.info("admin ==> {}", admin);
//            if (admin == null) {
//                return ResultUtils.error(ResultEnum.USERNAME_PASSWORD_ERROR.getCode(), ResultEnum.USERNAME_PASSWORD_ERROR.getMsg());
//            }
//            log.info("Login admin ==> {}",admin);
//            setSession(admin);
//        } catch (Exception e) {
//            log.error("Login Error ==> {}", e.getMessage());
//            return ResultUtils.error(1004, "登录失败");
//        }
//        logService.save(new Log(username, "登录后台", new Date()));
//        return ResultUtils.success("登录成功");
//    }
//
//    @GetMapping(value = {"", "/index"})
//    public String index(Model model) {
//        log.info("定向主页");
//        Admin admin = (Admin) getUser4Session();
//        if (admin == null){
//            return "admin/login";
//        }
//        IndexDataVO indexData = adminService.queryIndexData();
//        IPage<Log> logList = logService.queryByAll(1, 10);
//        model.addAttribute("index", indexData);
//        model.addAttribute("logList", logList.getRecords());
//        return "admin/index";
//    }
//}
